package com.example.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Date 2022/6/23
 * @Created by Jonathan
 */
@Slf4j
public class LockDemo {
    public static void main(String[] args) {
        final ReentrantLock lock = new ReentrantLock(true);
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            new Thread(() -> {
                lock.lock();
                try {
                    log.info("i --->{}", finalI);
                    TimeUnit.SECONDS.sleep(5);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }

            }, "thread" + i).start();
        }
    }
}
